package integration

import (
	"context"
	compb "gitee.com/micro-services/protocol/common"
	pb "gitee.com/micro-services/protocol/room/api"
	mpb "gitee.com/micro-services/protocol/room/server"
)

func CreateRoom(ctx context.Context, r *pb.APICreateRoomReq) (*pb.APICreateRoomRsp, error) {
	rsp, err := GetProxyManger().RoomClientProxy.CreateRoom(ctx, &mpb.CreateRoomReq{
		Info: &compb.RoomInfo{
			RoomID:            r.Info.RoomID,
			HotelID:           r.Info.HotelID,
			Floor:             r.Info.Floor,
			RoomNo:            r.Info.RoomNo,
			RoomImage:         r.Info.RoomImage,
			SmartImage:        r.Info.SmartImage,
			DetailImages:      r.Info.DetailImages,
			Detail:            r.Info.Detail,
			Price:             r.Info.Price,
			MarketPrice:       r.Info.MarketPrice,
			ActivityPrice:     r.Info.ActivityPrice,
			RoomType:          r.Info.RoomImage,
			ToiletNum:         r.Info.ToiletNum,
			BathroomDevice:    r.Info.BathroomDevice,
			ConvenienceDevice: r.Info.ConvenienceDevice,
			OtherDevice:       r.Info.OtherDevice,
			Source:            r.Info.Source,
			PackageID:         r.Info.PackageID,
			IsOpen:            r.Info.IsOpen,
		},
	})
	if err != nil {
		return nil, err
	}

	return &pb.APICreateRoomRsp{
		RetCode: rsp.RetCode,
		Msg:     rsp.Msg,
	}, nil
}

func UpdateRoomInfo(ctx context.Context, r *pb.APIUpdateRoomInfoReq) (*pb.APIUpdateRoomInfoRsp, error) {
	rsp, err := GetProxyManger().RoomClientProxy.UpdateRoom(ctx, &mpb.UpdateRoomReq{
		RoomID:            r.RoomID,
		Floor:             r.Floor,
		RoomNo:            r.RoomNo,
		RoomImage:         r.RoomImage,
		SmartImage:        r.SmartImage,
		DetailImages:      r.DetailImages,
		Detail:            r.Detail,
		Price:             r.Price,
		MarketPrice:       r.MarketPrice,
		ActivityPrice:     r.ActivityPrice,
		RoomType:          r.RoomType,
		ToiletNum:         r.ToiletNum,
		BathroomDevice:    r.BathroomDevice,
		ConvenienceDevice: r.ConvenienceDevice,
		OtherDevice:       r.OtherDevice,
		IsOpen:            r.IsOpen,
	})
	if err != nil {
		return nil, err
	}

	return &pb.APIUpdateRoomInfoRsp{
		RetCode: rsp.RetCode,
		Msg:     rsp.Msg,
	}, nil
}

func DeleteRoom(ctx context.Context, r *pb.APIDeleteRoomReq) (*pb.APIDeleteRoomRsp, error) {
	rsp, err := GetProxyManger().RoomClientProxy.DeleteRoom(ctx, &mpb.DeleteRoomReq{
		RoomID: r.RoomID,
	})
	if err != nil {
		return nil, err
	}

	return &pb.APIDeleteRoomRsp{
		RetCode: rsp.RetCode,
		Msg:     rsp.Msg,
	}, nil
}

func BatchRoomInfo(ctx context.Context, r *pb.APIBatchRoomInfoReq) (*pb.APIBatchRoomInfoRsp, error) {
	rsp, err := GetProxyManger().RoomClientProxy.BatchRoomInfo(ctx, &mpb.BatchRoomInfoReq{
		RoomID: r.RoomID,
	})
	if err != nil {
		return nil, err
	}

	info := make([]*compb.RoomInfo, 0)
	if rsp.GetInfo() != nil {
		for _, v := range rsp.Info {
			info = append(info, &compb.RoomInfo{
				RoomID:            v.RoomID,
				HotelID:           v.HotelID,
				Floor:             v.Floor,
				RoomNo:            v.RoomNo,
				RoomImage:         v.RoomImage,
				SmartImage:        v.SmartImage,
				DetailImages:      v.DetailImages,
				Detail:            v.Detail,
				Price:             v.Price,
				MarketPrice:       v.MarketPrice,
				ActivityPrice:     v.ActivityPrice,
				RoomType:          v.RoomType,
				ToiletNum:         v.ToiletNum,
				BathroomDevice:    v.BathroomDevice,
				ConvenienceDevice: v.ConvenienceDevice,
				OtherDevice:       v.OtherDevice,
				Source:            v.Source,
				PackageID:         v.PackageID,
				IsOpen:            v.IsOpen,
			})
		}
	}

	return &pb.APIBatchRoomInfoRsp{
		Info: info,
	}, nil
}

func RoomList(ctx context.Context, r *pb.APIRoomListReq) (*pb.APIRoomListRsp, error) {
	rsp, err := GetProxyManger().RoomClientProxy.SearchRoom(ctx, &mpb.SearchRoomReq{
		HotelID:       r.HotelID,
		Floor:         r.Floor,
		RoomNo:        r.RoomNo,
		Price:         r.Price,
		MarketPrice:   r.MarketPrice,
		ActivityPrice: r.ActivityPrice,
		RoomType:      r.RoomType,
		IsOpen:        r.IsOpen,
		Offset:        r.Offset,
		Limit:         r.Limit,
	})
	if err != nil {
		return nil, err
	}

	return &pb.APIRoomListRsp{
		List:  rsp.List,
		Count: rsp.Count,
	}, nil
}
